package com.ocom.payconfig.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ocom.common.entity.pay.EntityXfRecordProcessExtend;
import com.ocom.common.enums.DataSourceEnum;
import com.ocom.common.enums.MasterSlaveEnum;
import com.ocom.mysql.aspect.DataSourceAspect;
import com.ocom.payconfig.dao.IDaoXfRecordProcessExtend;
import com.ocom.payconfig.service.XfRecordProcessExtendService;
import org.springframework.stereotype.Service;

@Service
public class XfRecordProcessExtendServiceImpl extends ServiceImpl<IDaoXfRecordProcessExtend, EntityXfRecordProcessExtend> implements XfRecordProcessExtendService {
    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB, isMaster = MasterSlaveEnum.MASTER,companyId = "companyId")
    public boolean saveOrUpdate(Long companyId, EntityXfRecordProcessExtend entityXfRecordProcessExtend) {
        EntityXfRecordProcessExtend entityXfRecordProcessExtendA = this.getOne(Wrappers.lambdaQuery(EntityXfRecordProcessExtend.class)
                .eq(EntityXfRecordProcessExtend::getComId,entityXfRecordProcessExtend.getComId())
                .eq(EntityXfRecordProcessExtend::getOrderId,entityXfRecordProcessExtend.getOrderId())
                .eq(EntityXfRecordProcessExtend::getOrderNo,entityXfRecordProcessExtend.getOrderNo())
        );
        if(ObjectUtil.isNotEmpty(entityXfRecordProcessExtendA)){
            entityXfRecordProcessExtend.setId(entityXfRecordProcessExtendA.getId());
            return this.updateById(entityXfRecordProcessExtend);
        }else {
            return this.save(entityXfRecordProcessExtend);
        }
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB, isMaster = MasterSlaveEnum.SLAVE,companyId = "companyId")
    public EntityXfRecordProcessExtend getByOrderNo(Long companyId, String orderNo) {
        return this.getOne(Wrappers.lambdaQuery(EntityXfRecordProcessExtend.class)
                .eq(EntityXfRecordProcessExtend::getComId,companyId)
                .eq(EntityXfRecordProcessExtend::getOrderNo,orderNo).last("limit 1"));
    }
}
